package com.hxzy.module.commondity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.module.commodity.api.classifications.req.ClassificationSearchReq;
import com.hxzy.module.commodity.api.classifications.resp.ClassifiedManagementResp;
import com.hxzy.module.commondity.entity.ClassifiedManagement;
import com.hxzy.module.commondity.service.ClassifiedManagementService;
import com.hxzy.module.commondity.mapper.ClassifiedManagementMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 11452
* @description 针对表【classified_management(分类管理表)】的数据库操作Service实现
* @createDate 2024-01-23 15:16:09
*/
@Service
public class ClassifiedManagementServiceImpl extends ServiceImpl<ClassifiedManagementMapper, ClassifiedManagement>
    implements ClassifiedManagementService{

    /**
     * 获得分类列表分页信息
     * @param req
     * @return
     */
    @Override
    public PageInfo<ClassifiedManagementResp> getClassifiedPage(ClassificationSearchReq req) {
        //当前登录的商家ID
        long merchantId=1L;

        //分页查询商品主分类
        PageInfo<ClassifiedManagement> pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(() -> {
                     this.baseMapper.getClassifiedPage(req,merchantId);
                });

        //最终返回的对象
        PageInfo<ClassifiedManagementResp>  respPageInfo=new PageInfo<>();

        //判断是否要查询子分类
        if(pageInfo.getList().size()>0){
            //拿到主键id
            List<Long> primaryKeyList = pageInfo.getList().stream().map(ClassifiedManagement::getId).collect(Collectors.toList());
            //查询 主分类下面的子分类
            LambdaQueryWrapper<ClassifiedManagement> childQueryWrapper=new LambdaQueryWrapper<>();
            childQueryWrapper.in(ClassifiedManagement::getParentId,primaryKeyList);
            childQueryWrapper.eq(ClassifiedManagement::getDisable,false);
            childQueryWrapper.select(ClassifiedManagement::getId,ClassifiedManagement::getClassificationName,ClassifiedManagement::getParentId);
            List<ClassifiedManagement> childList = this.list(childQueryWrapper);

            //循环主类，查询子类，构造 ClassifiedManagementResp对象
            List<ClassifiedManagementResp>  respList= pageInfo.getList().stream().map( p-> createClassifiedManagementResp(p,childList)  ).collect(Collectors.toList());
            respPageInfo.setList(respList);
        }else{
            respPageInfo.setList(new ArrayList<ClassifiedManagementResp>());
        }
        //最终复制其它的分页属性,忽略list
        BeanUtils.copyProperties(pageInfo,respPageInfo,"list");
        return respPageInfo;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public ClassifiedManagementResp getClassified(Long id) {
        ClassifiedManagement parent = this.getById(id);
        //查询父节点下面所有的子节点
        LambdaQueryWrapper<ClassifiedManagement> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ClassifiedManagement::getParentId, id);
        queryWrapper.eq(ClassifiedManagement::getDisable,false);
        queryWrapper.select(ClassifiedManagement::getId,ClassifiedManagement::getClassificationName,ClassifiedManagement::getParentId);
        List<ClassifiedManagement> childList = this.list(queryWrapper);

        return createClassifiedManagementResp(parent,childList);
    }

    /**
     * 通过父类和子类集合构造自定义对象
     * @param p  父类
     * @param childList 子类列表
     * @return
     */
    private ClassifiedManagementResp createClassifiedManagementResp(ClassifiedManagement p, List<ClassifiedManagement> childList) {
        ClassifiedManagementResp  resp=new ClassifiedManagementResp();
        //采用 MapStruct来实现
        BeanUtils.copyProperties(p,resp);

        //在内存中过滤子类的数据
        List<ClassifiedManagementResp.SubClassified> childs = childList.stream()
                .filter(s -> s.getParentId().equals(p.getId()))
                .map(s -> new ClassifiedManagementResp.SubClassified(s.getId(), s.getClassificationName()))
                .collect(Collectors.toList());
        resp.setSubClassified(childs);

        return resp;
    }
}




